package com.hyfrogx.modules.manage.service.imitation.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.modules.manage.dao.imitation.ImitationDao;
import com.hyfrogx.modules.manage.entity.HyChannelApkEntity;
import com.hyfrogx.modules.manage.entity.imitation.ImitationEntity;
import com.hyfrogx.modules.manage.form.imitation.addImitationForm;
import com.hyfrogx.modules.manage.service.HyChannelApkService;
import com.hyfrogx.modules.manage.service.imitation.ImitationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Luo
 * @Date: 2019/12/17 13:36
 */
@Service
public class ImitationServiceImpl extends ServiceImpl<ImitationDao, ImitationEntity> implements ImitationService {

    @Autowired
    HyChannelApkService apkService;


    @Override
    public Page getList(QueryExt queryExt) {
        Map map = new HashMap();
        map.putAll(queryExt.getCondition());
        queryExt.setRecords(this.baseMapper.getList(map,queryExt));
        return queryExt;
    }

    @Override
    public Map getInfo(Integer id) {
        ImitationEntity entity = this.getById(id);
        Integer[] array = Convert.toIntArray(entity.getChannelNum());
        Map<String,Object> map = new HashMap();
        map.put("imitation",entity);
        if(array!=null){
            for (int channelId : array){
                HyChannelApkEntity apkEntity = apkService.getById(channelId);
                map.put("channel",apkEntity);
            }
        }
        return map;
    }

    @Override
    public Boolean save(addImitationForm form) {
        ImitationEntity entity = new ImitationEntity();
        BeanUtils.copyProperties(form, entity);
        entity.setCreateDate(new Date());
        boolean save = this.save(entity);
        if (save) {
            return true;
        } else {
            return false;
        }
    }
}
